package com.elite.groovy.callback
import groovy.json.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import com.elite.groovy.util.EliteBean;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wisdge.dataservice.Result;
import groovy.swing.impl.DefaultAction;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import cn.com.tpUtil.GetXMLValue

import com.elite.groovy.util.Util;
import cn.com.kh.HttpClientProxy;


public class apiBaoAn implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(apiBaoAn.class);
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");
		log.debug("start apiBaoAn-----------------");
		log.debug("request--:" + request);

		def REQUEST_TYPE;
		def RESPONSE_CODE;
		def ERROR_MESSAGE;
		def CUSTOMER_GUID;
		def FULL_POLICY_NO;
		def JQ_POLICY_NO;
		def SY_POLICY_NO;		
		def REPORT_MOBILE;
		def OPERATE_TIME;
		def coreResult="";
		String getMethod = request.getMethod();
		String callJson = "";
		BufferedReader iny = null;		

		log.debug("getMethod:" + getMethod);
		if (getMethod.equals("GET")) {
			callJson = request.getParameter("data");
			log.debug("原始Get请求参数:" + callJson);
		} else {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				callJson += line;
			}
			log.debug("原始Post请求参数:" + callJson);
		}
  
		try {
			
			def querySql1 = "select webserviceurl from sysc_interface  where webtype='BAMOBILE' and rownum=1";
			def queryList1=eliteBean.chaxun_sql(querySql1, "dataSource");
			def queryMap1=queryList1[0];
			def httpURL=queryMap1.get("webserviceurl");
			
		   CUSTOMER_GUID=GetXMLValue.readStringXml(callJson, "CUSTOMER_GUID");
		   JQ_POLICY_NO=GetXMLValue.readStringXml(callJson, "JQ_POLICY_NO");
		   SY_POLICY_NO=GetXMLValue.readStringXml(callJson, "SY_POLICY_NO");
		   FULL_POLICY_NO=GetXMLValue.readStringXml(callJson, "FULL_POLICY_NO");
		   
		   def sendBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><PACKET TYPE=\"REQUEST\" VERSION=\"1.0\"><HEAD><REQUEST_TYPE>X1</REQUEST_TYPE></HEAD><BODY><CUSTOMER_GUID>"+CUSTOMER_GUID+"</CUSTOMER_GUID><JQ_POLICY_NO>"+JQ_POLICY_NO+"</JQ_POLICY_NO><SY_POLICY_NO>"+SY_POLICY_NO+"</SY_POLICY_NO><FULL_POLICY_NO>"+FULL_POLICY_NO+"</FULL_POLICY_NO></BODY></PACKET>";
		   
		   log.debug("系统转发核心系统调用发送URL:" + httpURL + " 发送报文:" + sendBody);
		   coreResult = HttpClientProxy.sendPost(httpURL, sendBody, "", 30000);
		   log.debug("系统转发核心系统调用返回结果:" + coreResult);			   

		} catch (Exception e) {
			log.debug("系统转发核心系统调用异常:" + e.getMessage());
		}
		
		return coreResult;
	}
	
}